package com.hanlin.backend.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hanlin.backend.common.dto.SettingDto;
import com.hanlin.backend.common.entity.HlSetting;
import com.hanlin.backend.common.service.HlSettingService;
import com.hanlin.backend.common.util.ResultCommon;
import com.hanlin.backend.common.util.TableCommon;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 全局参数表 前端控制器
 * </p>
 *
 * @author hl.yuan
 * @since 2021-01-06
 */
@RestController
@Slf4j
@Api(value = "/frontend/api/setting", tags = "全局参数")
public class HlSettingController {

    @Autowired
    private HlSettingService hlSettingService;

    @GetMapping("/frontend/api/setting/tableList")
    @ApiOperation("获取全局参数列表")
    public ResultCommon<TableCommon<HlSetting>> getTable(SettingDto dto) {
        IPage<HlSetting> hlEnumList = hlSettingService.getTable(dto);
        return dto.build(hlEnumList);
    }

    @PostMapping("/frontend/api/setting/save")
    @ApiOperation("保存全局参数")
    public ResultCommon save(@RequestBody HlSetting setting) {
        ResultCommon resultCommon = new ResultCommon();
        try {
            hlSettingService.saveSetting(setting);
        } catch (Exception ex) {
            resultCommon.setCode("500");
            log.error("保存失败", ex);
        }
        return resultCommon;
    }

    @GetMapping("/frontend/api/setting/delete")
    @ApiOperation("根据ID全局参数")
    public ResultCommon delete(int id) {
        ResultCommon resultCommon = new ResultCommon();
        try {
            hlSettingService.deleteSetting(id);
        } catch (Exception ex) {
            resultCommon.setCode("500");
            log.error("删除失败", ex);
        }
        return resultCommon;
    }


    @GetMapping("/frontend/api/setting/{id}")
    @ApiOperation("根据ID获取setting对象")
    public ResultCommon getSetting(@PathVariable("id") int id) {
        ResultCommon<HlSetting> resultCommon = new ResultCommon();
        try {
            HlSetting setting = hlSettingService.getById(id);
            resultCommon.setData(setting);
        } catch (Exception ex) {
            resultCommon.setCode("500");
            log.error("获取失败", ex);
        }
        return resultCommon;
    }

    @GetMapping("/frontend/api/setting/getSetting")
    @ApiOperation("根据model和name获取setting对象")
    public ResultCommon getSettingByModelAndName(HlSetting setting) {
        ResultCommon<HlSetting> resultCommon = new ResultCommon();
        try {
            HlSetting hlSetting = hlSettingService.getSetting(setting.getModel(), setting.getName());
            resultCommon.setData(hlSetting);
        } catch (Exception ex) {
            resultCommon.setCode("500");
            log.error("获取失败", ex);
        }
        return resultCommon;
    }

    @PostMapping("/frontend/api/setting/saveMail")
    @ApiOperation("保存邮件签名")
    public ResultCommon saveMail(@RequestBody HlSetting setting) {
        ResultCommon<HlSetting> resultCommon = new ResultCommon();
        try {
            hlSettingService.saveMail(setting.getModel(), setting.getName(), setting.getValue());
        } catch (Exception ex) {
            resultCommon.setCode("500");
            log.error("获取失败", ex);
        }
        return resultCommon;
    }


}

